﻿function isArray(obj) {
    return (typeof obj == 'object') && obj.constructor == Array;
}

function isString(str) {
    return (typeof str == 'string') && str.constructor == String;
}

function isNumber(obj) {
    return (typeof obj == 'number') && obj.constructor == Number;
}

function isDate(obj) {
    return (typeof obj == 'object') && obj.constructor == Date;
}

function isFunction(obj) {
    return ("undefined" != typeof obj && obj != null && typeof obj == 'function') && obj.constructor == Function;
}

function isObject(obj) {
    if ("undefined" == typeof obj || obj == null) {
        return false;
    }

    return (typeof obj == 'object');
}

//必需是整数
function isInt(str) {
    if (!isObject(str)) //判断对象是否存在
    {
        return false;
    }

    var reg = /^(-|\+)?\d+$/;
    return reg.test(str);
}

//必需是小数
function isFloat(str) {
    if (!isObject(str)) //判断对象是否存在
    {
        return false;
    }

    var reg = /^(-|\+)?\d+\.\d*$/;
    return reg.test(str);
}

//判断是否为空 
function isEmpty(str) {
    if ("undefined" == typeof str || str == null || $.trim(str) == "") {
        return true;
    } else {
        return false;
    }
}

function getPropertyEx(obj, propName, defaultValue) {
    if (obj == null) {
        return defaultValue;
    }

    if (isObject(obj) && obj.hasOwnProperty(propName)) {
        return obj[propName];
    } else {
        return defaultValue;
    }
}

function startWith(str, checkValue) {
    if (checkValue == null || checkValue == "" || str.length == 0 || checkValue.length > str.length)
        return false;
    if (str.substr(0, checkValue.length) == checkValue)
        return true;
    else
        return false;
    return true;
}

function endWith(str, checkValue) {
    if (checkValue == null || checkValue == "" || str.length == 0 || checkValue.length > str.length)
        return false;
    if (str.substring(str.length - checkValue.length) == checkValue)
        return true;
    else
        return false;
    return true;
}

function parseIntEx(value, defaultValue) {
    if (typeof defaultValue == 'undefined') {
        defaultValue = 0;
    }

    var v = parseInt(value);

    if (isNaN(v)) {
        return defaultValue;
    } else {
        return v;
    }
}

function parseDecimalEx(value, defaultValue) {

    if (typeof defaultValue == 'undefined') {
        defaultValue = new Decimal(0);
    }

    try {
        var newValue = new Decimal(value);

        return newValue;
    } catch (e) {
        return defaultValue;
    }
}

function parseFloatEx(value, defaultValue) {
    if (typeof defaultValue == 'undefined') {
        defaultValue = 0;
    }

    var v = parseFloat(value);

    if (isNaN(v)) {
        return defaultValue;
    } else {
        return v;
    }
}

QueryString = {
    data: {},
    Initial: function () {
        var aPairs, aTmp;
        var queryString = new String(window.location.search);
        queryString = queryString.substr(1, queryString.length); //remove   "?"     
        aPairs = queryString.split("&");
        for (var i = 0; i < aPairs.length; i++) {
            aTmp = aPairs[i].split("=");
            this.data[aTmp[0]] = aTmp[1];
        }
    },
    GetValue: function (key) {
        try {
            return this.data[key];
        } catch (e) {
            return "";
        }
    }
};

String.prototype.format = function () {
    var formatted = this;

    if (arguments.length === 1 && isObject(arguments)) {
        var args = arguments[0];
        for (var arg in args) {
            var regex = new RegExp("\{" + arg + "\}", "gm");
            formatted = formatted.replace(regex, args[arg]);
        }
    } else {
        for (var arg in arguments) {
            formatted = formatted.replace("/\{" + arg + "\}/g", arguments[arg]);
        }
    }


    return formatted;
};

String.prototype.toStringEx = function (defaultValue) {

    var formatted = this;

    if (arguments.length === 1 && isObject(arguments)) {
        var args = arguments[0];
        for (var arg in args) {
            formatted = formatted.replace("{" + arg + "}", args[arg]);
        }
    } else {
        for (var arg in arguments) {
            formatted = formatted.replace("{" + arg + "}", arguments[arg]);
        }
    }


    return formatted;
};

function addUrlPara(url, name, value) {
    var currentUrl = url.split('#')[0];
    if (/\?/g.test(currentUrl)) {
        if (/name=[-\w]{4,25}/g.test(currentUrl)) {
            currentUrl = currentUrl.replace(/name=[-\w]{4,25}/g, name + "=" + value);
        } else {
            currentUrl += "&" + name + "=" + value;
        }
    } else {
        currentUrl += "?" + name + "=" + value;
    }
    if (window.location.href.split('#')[1]) {
        window.location.href = currentUrl + '#' + window.location.href.split('#')[1];
    } else {
        window.location.href = currentUrl;
    }
}

var userAgent = navigator.userAgent.toLowerCase();
// Figure out what browser is being used 
jQuery.browser = {
    version: (userAgent.match(/.+(?:rv|it|ra|ie)[\/: ]([\d.]+)/) || [])[1],
    safari: /webkit/.test(userAgent),
    opera: /opera/.test(userAgent),
    msie: /msie/.test(userAgent) && !/opera/.test(userAgent),
    mozilla: /mozilla/.test(userAgent) && !/(compatible|webkit)/.test(userAgent)
};

function select(list, selectFunc) {
    var tmp = [];

    for (var key in list) {
        tmp.push(selectFunc(key, list[key]));
    }

    return tmp;
}

/**
 * 根据checkfunc参数,返回list中第一个符合checkfunc为true的值,如果不存在,则返回defaultValue的值
 * @param {any} list 数据源列表,需为一个array或者一个object
 * @param {any} checkFunc 检查函数,如果匹配,则返回true,不匹配则返回false
 * @param {any} defaultValue 当查找不到值时,返回该参数的值,如果不传,则为null
 */
function firstOrDefault(list, checkFunc, defaultValue) {

    var result;

    $.each(list,
        function(key, value) {
            if (checkFunc(key, value)) {
                result = value;
                return false;
            }
        });

    if (result) {
        return result;
    } else {
        if (defaultValue) {
            return defaultValue;
        } else {
            return null;
        }
    }
}

/**
 * 获取控件的值,如果是input,则获取value,如果是普通控件,则获取text
 * @param {any} el
 */
function getElementValue(el) {
    if ($(el).is(":input")) {
        return $(el).val();
    } else {
        return $(el).text();
    }
}